﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CommonFSM : MonoBehaviour
{
    protected GameObject m_ObjCloseAtkTarget = null;
    public GameObject m_ObjTarget = null;
    public Vector3 m_vecDest = new Vector3(0.0f, 0.0f, 0.0f);

    [HideInInspector]
    public List<int> m_BestList;

    private int m_iPrev_BestListIdx = 0;

    public int m_iCurrentTileIdx;
    [HideInInspector]
    public int m_iPrevTIleIdx;
    [HideInInspector]
    public int m_iPrevTileOption;

    //----bool
    protected bool m_bAimEnable = false;
    protected bool m_bTauntEnbale = true;
    protected bool m_bSupply = false;

    protected bool EnemyTarget_Find(string strFactionTag) //시야내에 가장 가까운 타겟을 찾아냄
    {
        Collider2D[] CollObjs = (Physics2D.OverlapCircleAll(gameObject.transform.position,
            gameObject.GetComponent<UnitStatus>().m_unitData.fSightRange));

        float distance = Mathf.Infinity;


        foreach (Collider2D Obj in CollObjs)
        {
            if (Obj.gameObject.tag == strFactionTag)
            {
                if (Obj.gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState != BASEUNIT_STATE.DEAD)
                {
                    Vector3 diff = Obj.gameObject.transform.position - gameObject.transform.position;
                    float curDistance = diff.sqrMagnitude;
                    if (curDistance < distance)
                    {
                        m_ObjTarget = Obj.gameObject;
                        distance = curDistance;
                    }
                }

            }
        }

        if (m_ObjTarget == null)
            return false;
        else
            return true;
    }

    protected virtual BASEUNIT_STATE AStarMove()
    {
        if (m_BestList == null || m_BestList.Count == 0)
        {
            if (gameObject.tag == "Player")
            {
                if (m_bSupply)
                    return BASEUNIT_STATE.SUPPLY;
                else
                    return BASEUNIT_STATE.IDLE;

            }
            else
                return BASEUNIT_STATE.IDLE;
        }

        GameObject NextTile = GameObject.Find("DebugTile").GetComponent<Background>().m_ListTile[m_BestList[0]];

        if (NextTile.GetComponent<Tile>().m_iOption == (int)TILE_OPTION.BLOCK || NextTile.GetComponent<Tile>().m_iOption == (int)TILE_OPTION.BUILD)
        {
            Calculate.AStarStart(gameObject, m_vecDest, ref m_BestList, m_iPrevTIleIdx, m_iPrevTileOption);

        }

        Vector2 vecNextDest = NextTile.transform.position;

        Calculate.RotToDest(gameObject, vecNextDest);
        Calculate.MoveToDest(gameObject, vecNextDest, gameObject.GetComponent<UnitStatus>().m_unitData.fSpeed);

        if (Vector2.Distance(gameObject.transform.position, vecNextDest) < 0.001f)
        {

            //GameObject.Find("DebugTile").GetComponent<Background>().m_ListTile[m_iPrev_BestListIdx].GetComponent<Tile>().m_iOption = (int)TILE_OPTION.NORMAL;

            gameObject.transform.position = vecNextDest;
            m_iCurrentTileIdx = m_BestList[0];

            m_iPrev_BestListIdx = m_BestList[0];

            m_BestList.Remove(m_BestList[0]);

            if (m_BestList.Count == 0)
            {
                m_BestList.Clear();
                m_BestList = null;
            }

        }

        return BASEUNIT_STATE.MOVE;
    }
}
